package com.heima.wemedia.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dto.WmNewsDto;
import com.heima.model.wemedia.dto.WmNewsPageReqDto;
import com.heima.model.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 自媒体文章表(WmNews)表控制层
 *
 * @author makejava
 * @since 2022-04-10 22:00:15
 */
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {
    /**
     * 服务对象
     */
    @Resource
    private WmNewsService wmNewsService;

    /**
     * 分页查询
     *
     * @param wmNews 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
/*    @GetMapping
    public ResponseEntity<Page<WmNews>> queryByPage(WmNews wmNews, PageRequest pageRequest) {
        return ResponseEntity.ok(this.wmNewsService.queryByPage(wmNews, pageRequest));
    }*/

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<WmNews> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.wmNewsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param wmNews 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<WmNews> add(WmNews wmNews) {
        return ResponseEntity.ok(this.wmNewsService.insert(wmNews));
    }

    /**
     * 编辑数据
     *
     * @param wmNews 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<WmNews> edit(WmNews wmNews) {
        return ResponseEntity.ok(this.wmNewsService.update(wmNews));
    }


    /**
     * 自媒体文章发布 , 修改 , 保存草稿
     *
     * @param dto
     * @return
     */
    @PostMapping(path = "/submit")
    public ResponseResult submit(@RequestBody WmNewsDto dto) {
        return wmNewsService.submit(dto);
    }

    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.list(dto);
    }

    /**
     * 查询单个文章详情
     *
     * @param newsId
     * @return
     */
    @GetMapping("/one/{newsId}")
    public ResponseResult one(@PathVariable("newsId") Long newsId) {
        WmNews wmNews = wmNewsService.queryById(newsId);
        return ResponseResult.okResult(wmNews);
    }

    /**
     * 删除数据
     *
     * @param newsId 主键
     * @return 删除是否成功
     */
    @GetMapping("del_news/{newsId}")
    public ResponseResult deleteById(@PathVariable("newsId") Long newsId) {
        return ResponseResult.okResult(wmNewsService.deleteById(newsId));
    }

}

